//
//  LZSHisRestViewController.m
//  IOSProject
//
//  Created by lzs on 2018/4/20.
//  Copyright © 2018年 com.linzhisheng. All rights reserved.
//

#import "LZSHisRestViewController.h"
#import "LZSHistoryModel.h"
#import "LZSHisRestViewCell.h"

#import "LZSCaipiaohistoryModel.h"

@interface LZSHisRestViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) NSMutableArray *realArray;

@property (nonatomic, strong) NSDictionary *dataArray;
@end

@implementation LZSHisRestViewController

-(NSDictionary *)dataArray{
    if (!_dataArray) {
        _dataArray = @{
                       @"lotteryResList": @[
                               @{
                                   @"lottery_id": @"ssq",
                                   @"lottery_res": @"03,08,11,14,18,23,16",
                                   @"lottery_no": @"18028",
                                   @"lottery_date": @"2018-03-13",
                                   @"lottery_exdate": @"2018-05-11",
                                   @"lottery_sale_amount": @"352,015,830",
                                   @"lottery_pool_amount": @"578,094,167"
                                   },
                               @{
                                   @"lottery_id": @"ssq",
                                   @"lottery_res": @"02,07,09,14,18,28,05",
                                   @"lottery_no": @"18027",
                                   @"lottery_date": @"2018-03-11",
                                   @"lottery_exdate": @"2018-05-09",
                                   @"lottery_sale_amount": @"386,579,026",
                                   @"lottery_pool_amount": @"561,561,972"
                                   },
                               @{
                                   @"lottery_id": @"ssq",
                                   @"lottery_res": @"04,07,12,14,26,32,04",
                                   @"lottery_no": @"18026",
                                   @"lottery_date": @"2018-03-08",
                                   @"lottery_exdate": @"2018-05-06",
                                   @"lottery_sale_amount": @"341,958,742",
                                   @"lottery_pool_amount": @"537,930,372"
                                   },
                               @{
                                   @"lottery_id": @"ssq",
                                   @"lottery_res": @"04,13,16,19,21,25,14",
                                   @"lottery_no": @"18025",
                                   @"lottery_date": @"2018-03-06",
                                   @"lottery_exdate": @"2018-05-04",
                                   @"lottery_sale_amount": @"341,614,936",
                                   @"lottery_pool_amount": @"513,258,146"
                                   },
                               @{
                                   @"lottery_id": @"ssq",
                                   @"lottery_res": @"11,19,22,26,31,32,02",
                                   @"lottery_no": @"18024",
                                   @"lottery_date": @"2018-03-04",
                                   @"lottery_exdate": @"2018-05-02",
                                   @"lottery_sale_amount": @"373,443,496",
                                   @"lottery_pool_amount": @"436,102,047"
                                   }
                               ],
                       @"page": @1,
                       @"pageSize": @5,
                       @"totalPage": @447
                       };
    }
    return _dataArray;
}
-(NSMutableArray *)realArray{
    if (!_realArray) {
        _realArray = [NSMutableArray array];
    }
    return _realArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    //    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = [UIColor lightGrayColor];
    //
    self.tableView.estimatedRowHeight = 80;
    
    self.tableView.separatorColor = [UIColor colorWithHexString:@"#c8c8c8"];
    
    //默认【下拉刷新】
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    //默认【上拉加载】
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    //
    self.tableView.tableFooterView = [UIView new];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).mas_offset(0);
    }];
    
    //    [self headerSdcycleScrollView];
    
    //
    [self.tableView.mj_header beginRefreshing];
}

-(void)refresh{
    self.page = 0;
//    [self lotterType];
    [self caipiaohistory];
}

-(void)loadMore{
    //    sleep(2);
//    [self.tableView.mj_header endRefreshing];
//    [self.tableView.mj_footer endRefreshing];
//    [self lotterType];
     [self caipiaohistory];
}

-(void)lotterType{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"91b22ffb918dbb3a2b5f98be27009a47" forKey:@"key"];
    [params setValue:self.lottery_id forKey:@"lottery_id"];
    [params setValue:[NSString stringWithFormat:@"%ld",(long)self.page] forKey:@"page"];
    [self postData:@"http://apis.juhe.cn/lottery/history" param:params success:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
//        NSLog(@"%@",responseObject);
    
        LZSHistoryModel *datamodel = [LZSHistoryModel mj_objectWithKeyValues:responseObject[@"result"]];
//    LZSHistoryModel *datamodel = [LZSHistoryModel mj_objectWithKeyValues:self.dataArray];
        
        //是刷新还是加载
        if (self.page==1) {
            if (datamodel.lotteryResList.count==0) {
                //                self.noDataView.hidden = NO;
                self.tableView.hidden = YES;
            }else{
                //                self.noDataView.hidden = YES;
                self.tableView.hidden = NO;
            }
            
            [self.realArray removeAllObjects];
            [self.realArray addObjectsFromArray:datamodel.lotteryResList];
        }else{
            [self.realArray addObjectsFromArray:datamodel.lotteryResList];
        }
        
        //是否提示可加载
        if (datamodel.lotteryResList.count<10) {
            // 变为没有更多数据的状态
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            self.tableView.mj_footer.hidden = YES;
        }else{
            self.page++;
        }
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } error:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.view makeToast:error.domain duration:0.8 position:CSToastPositionCenter];
        NSLog(@"%@",error);
    }];
}

-(void)caipiaohistory{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"31771dfbcb6cda3d" forKey:@"appkey"];
    [params setValue:@"13" forKey:@"caipiaoid"];
    [params setValue:@"" forKey:@"issueno"];
    [params setValue:@"20" forKey:@"num"];
    [params setValue:[NSString stringWithFormat:@"%ld",(long)self.page] forKey:@"start"];
    [self postData:@"http://api.jisuapi.com/caipiao/history" param:params success:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        NSLog( @"responseObject%@",responseObject);
        //        NSLog(@"%@",responseObject);
        
//        LZSHistoryModel *datamodel = [LZSHistoryModel mj_objectWithKeyValues:responseObject[@"result"]];
        //    LZSHistoryModel *datamodel = [LZSHistoryModel mj_objectWithKeyValues:self.dataArray];
//         LZSHistoryModel *datamodel = [LZSHistoryModel mj_objectWithKeyValues:responseObject[@"result"]];
//        NSArray *array = responseObject[@"result"][@"list"];
//        LZSCaipiaohistoryModel *datamodel = [LZSCaipiaohistoryModel mj_objectWithKeyValues:array];
        LZSCaipiaohistoryModel *datamodel = [LZSCaipiaohistoryModel mj_objectWithKeyValues:responseObject[@"result"]];
        
        
        //是刷新还是加载
        if (self.page==0) {
            if (datamodel.list.count==0) {
                //                self.noDataView.hidden = NO;
                self.tableView.hidden = YES;
            }else{
                //                self.noDataView.hidden = YES;
                self.tableView.hidden = NO;
            }
            
            [self.realArray removeAllObjects];
            [self.realArray addObjectsFromArray:datamodel.list];
        }else{
            [self.realArray addObjectsFromArray:datamodel.list];
        }
        
        //是否提示可加载
        if (datamodel.list.count<20) {
            // 变为没有更多数据的状态
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            self.tableView.mj_footer.hidden = YES;
        }else{
            self.page++;
        }
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } error:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.view makeToast:error.domain duration:0.8 position:CSToastPositionCenter];
        NSLog(@"%@",error);
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.realArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LZSHisRestViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[LZSHisRestViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
//    cell.model = self.realArray[indexPath.row];
    cell.historyModel = self.realArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
